/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maze;

import java.awt.*;
import javax.swing.*;
import java.util.LinkedList;

/**
 *
 * @author Iskandar Sarkawi
 */
public class ImagePanel extends JPanel{

    private final static int CELL_WIDTH = 30;
    private final static int CELL_HEIGHT = 30;

    //this does not work in netbeans. not sure why?
    //Image grassImg = Toolkit.getDefaultToolkit().getImage("grass.png");
//    Image badbotImg = Toolkit.getDefaultToolkit().getImage("badbot.png");
//    Image goodbotImg = Toolkit.getDefaultToolkit().getImage("goodbot.png");
//    Image homeImg = Toolkit.getDefaultToolkit().getImage("home.png");
//    Image obstacleImg = Toolkit.getDefaultToolkit().getImage("obstacle.png");   

    Image grassImg = Toolkit.getDefaultToolkit().getImage( Maze.class.getResource("grass.png" ) );
    Image badbotImg = Toolkit.getDefaultToolkit().getImage(Maze.class.getResource("badbot.png"));
    Image goodbotImg = Toolkit.getDefaultToolkit().getImage(Maze.class.getResource("goodbot.png"));
    Image homeImg = Toolkit.getDefaultToolkit().getImage(Maze.class.getResource("home.png"));
    Image obstacleImg = Toolkit.getDefaultToolkit().getImage(Maze.class.getResource("obstacle.png"));    

    private Maze maze;

    /*
     * Constructor
     */
    public ImagePanel(Maze m){
        maze = m;
        
        this.setPreferredSize(new Dimension(m.getWidth()*30, m.getHeight()*30));
        
    }

    /*
     * Draws the images
     */
    public void paintComponent(Graphics g){

        super.paintComponent(g);
        
//        g.drawImage(grassImg, 50, 50, this);
//        g.drawImage(badbotImg, 70, 70, this);
//        g.drawImage(goodbotImg, 90, 90, this);
//        g.drawImage(homeImg, 110, 110, this);
//        g.drawImage(obstacleImg, 130, 130, this);

        //Get the information of the map
        Tile[][] map = maze.getMap();
        for(int row=0; row<maze.getWidth(); row++){
            for (int col=0; col<maze.getHeight(); col++){
                int cellRow = row * CELL_WIDTH;
                int cellCol = col * CELL_HEIGHT;

                //If it is passable then it's green grass
                //If it is not passable then it's an obstacles, put a rock on it
                if(map[row][col].isPassable())
                    g.drawImage(grassImg, cellRow, cellCol, this);
                else
                    g.drawImage(obstacleImg, cellRow, cellCol, this);;
            }
        }

        //Paint all the bad bots location
        LinkedList<Robot> badBots = maze.getBadBots();
        for(int i=0; i<badBots.size(); i++){
            Robot bad = badBots.get(i);
            int cellRow = bad.getX() * CELL_WIDTH;
            int cellCol = bad.getY() * CELL_HEIGHT;
            g.drawImage(badbotImg, cellRow, cellCol, this);;
        }

        //Paint the goal location
        g.drawImage(homeImg, maze.getGoalX()*CELL_WIDTH, maze.getGoalY()*CELL_HEIGHT , this);

        //Paint the good bot
        int cellRow = maze.getGoodBot().getX() * CELL_WIDTH;
        int cellCol = maze.getGoodBot().getY() * CELL_HEIGHT;
        g.drawImage(goodbotImg, cellRow, cellCol, this);

        //Let the user know the status of the game
        if(maze.getCondition() == maze.CONDITION_WIN){
            g.drawString("You cleared this stage!", maze.getWidth()/3 * CELL_WIDTH, maze.getHeight()/2 * CELL_HEIGHT);
        }else if(maze.getCondition() == maze.CONDITION_LOSE){
            g.drawString("You loose this game!", maze.getWidth()/3 * CELL_WIDTH, maze.getHeight()/2 * CELL_HEIGHT);
        }else if(maze.getCondition() == maze.CONDITION_NOTHING){
            g.drawString("You are still in the game!", maze.getWidth()/3 * CELL_WIDTH, maze.getHeight()/2 * CELL_HEIGHT);
        }

    }

}
